package com.king.factory;

import java.util.Arrays;
import java.util.Set;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AssignableTypeFilter;

/**
 * 
 * <p class="detail">
 * 功能：自定义类扫描
 * </p>
 * @ClassName: KingClassPathMapperScanner 
 * @version V1.0  
 * @date 2014年12月17日 
 * @author <a href="mailto:wangs@financegt.com">kings</a>
 */
public class KingClassPathMapperScanner extends ClassPathBeanDefinitionScanner  {

	public KingClassPathMapperScanner(BeanDefinitionRegistry registry,Class<?> kInterface) {
        super(registry);
        addIncludeFilter(new AssignableTypeFilter(kInterface));
    }

    @Override
    public Set<BeanDefinitionHolder> doScan(String... basePackages) {
        Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);

        if (beanDefinitions.isEmpty()) {
            logger.warn("No IRequest mapper was found in '" + Arrays.toString(basePackages)+ "' package. Please check your configuration.");
        }
        GenericBeanDefinition definition;
        for (BeanDefinitionHolder holder : beanDefinitions) {
            definition = (GenericBeanDefinition) holder.getBeanDefinition();
            definition.getPropertyValues().add("proxy",
                getRegistry().getBeanDefinition("kingProxy"));
            definition.getPropertyValues().add("kInterface", definition.getBeanClassName());
            definition.setBeanClass(KingBeanFactory.class);
        }

        return beanDefinitions;
    }

    /**
     * 默认不允许接口的,这里重写,覆盖下
     */
    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        return beanDefinition.getMetadata().isInterface()
               && beanDefinition.getMetadata().isIndependent();
    }
}
